package com.pzhu.dormitory.controller;


import com.pzhu.dormitory.entity.MenuEntity;
import com.pzhu.dormitory.service.MenuService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.R;
import com.pzhu.dormitory.utils.Rb;
import com.pzhu.dormitory.vo.MenuNode;
import com.pzhu.dormitory.vo.MenuRoleVo;
import com.pzhu.dormitory.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 菜单
 *
 * @author
 * @email
 * @date 2021-04-10 08:46:25
 */
@Slf4j
@RestController
@Api(value = "MenuController", tags = "菜单")
@RequestMapping("elective/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取菜单list")
    public R<MenuVo> list(@RequestParam Map<String, Object> params) {
        PageUtils<MenuEntity> page = menuService.queryPage(params);
        PageUtils<MenuVo> pageVo = new PageUtils<>();
        BeanUtils.copyProperties(page, pageVo);
        List<MenuEntity> menuEntities = page.getList();
        List<MenuVo> menuVos = menuEntities.stream().map(menuEntity -> {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(menuEntity, menuVo);
            MenuEntity menuServiceByPId = menuService.getById(menuEntity.getPid());
            Optional.ofNullable(menuServiceByPId).ifPresent(menuEntity1 -> menuVo.setPName(menuEntity1.getName()));
            return menuVo;
        }).collect(Collectors.toList());
        pageVo.setList(menuVos);
        // 设置菜单数据
        return Rb.a(MenuVo.class).success().page(pageVo).build();
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取一个菜单信息")
    public R<MenuEntity> info(@Validated @PathVariable("id") Long id) {
        MenuEntity menu = menuService.getById(id);
        return Rb.a(MenuEntity.class).success().withData(menu).build();
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存菜单信息")
    @PostMapping("/save")
    public R<MenuEntity> save(@Validated @RequestBody MenuEntity menu) {
        menuService.save(menu);
        return Rb.a(MenuEntity.class).success().build();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新菜单信息")
    @PutMapping("/update")
    public R<MenuEntity> update(@RequestBody MenuEntity menu) {
        menuService.updateById(menu);
        return Rb.a(MenuEntity.class).success().build();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除菜单信息")
    @DeleteMapping("/delete")
    public R<MenuEntity> delete(@RequestBody Long[] ids) {
        menuService.removeByIds(Arrays.asList(ids));
        return Rb.a(MenuEntity.class).success().build();
    }

    /**
     * load 菜单
     */
    @ApiOperation("加载菜单")
    @GetMapping("/load")
    public R<MenuNode> loadMenu() {
        MenuNode menuNode = menuService.loadMenu();
        return Rb.a(MenuNode.class).success().withData(menuNode).build();
    }

    /**
     * 查询用户角色
     */
    @ApiOperation(value = "查询用户角色")
    @GetMapping("/role/{menuId}")
    public R<MenuRoleVo> roles(@PathVariable String menuId) {
        MenuRoleVo menuRoleVo = menuService.loadRoles(menuId);
        return Rb.a(MenuRoleVo.class).success().withData(menuRoleVo).build();
    }

    @GetMapping("/options")
    @ApiOperation(value = "获取学生信息表list")
    public R<MenuEntity> options() {
        List<MenuEntity> list = menuService.lambdaQuery().eq(MenuEntity::getPid, -1L).list();
        return Rb.a(MenuEntity.class).success().list(list).build();
    }
}
